package com.its.us.web.utils.Tree;

import com.its.us.web.entity.sys.Menu;
import com.its.us.web.entity.sys.Role;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: master
 * @Date: 2020/11/5 18:23
 * @Version: 1.0
 */
public class TreeUtil {

    /**
     * getMenus
     * @param menus
     * @return
     */
    public static List<TreeMenu> getCurrentMenus(List<Menu> menus) {
        List<TreeMenu> treeMenus = new ArrayList<>();
        TreeMenu treeMenu;
        if (menus.size() != 0) {
            for (int i = 0; i < menus.size(); i++) {
                if (!StringUtils.isEmpty(menus.get(i).getParentId()) && "0".equals(menus.get(i).getParentId())) {
                    treeMenu = new TreeMenu();
                    treeMenu.setId(menus.get(i).getMenuId());
                    treeMenu.setText(menus.get(i).getMenuName());
                    treeMenu.setIconCls(menus.get(i).getIcons());
                    treeMenu.setChildren(getCurrentChildrenMenus(menus, menus.get(i).getMenuId()));
                    treeMenus.add(treeMenu);
                }
            }
        }
        return treeMenus;
    }

    private static List<TreeMenu> getCurrentChildrenMenus(List<Menu> menus, String menuId) {
        List<TreeMenu> treeMenus = new ArrayList<>();
        TreeMenu treeMenu;
        if (menus.size() != 0) {
            for (int i = 0; i < menus.size(); i++) {
                if (!StringUtils.isEmpty(menus.get(i).getParentId()) && 0 != Integer.parseInt(menus.get(i).getParentId())) {
                    if (menuId.equals(menus.get(i).getParentId()) && "0".equals(menus.get(i).getIsShow()) && "1".equals(menus.get(i).getIsButton())) { // 不包含按钮的
                        treeMenu = new TreeMenu();
                        treeMenu.setId(menus.get(i).getMenuId());
                        treeMenu.setParentId(menuId);
                        treeMenu.setText(menus.get(i).getMenuName());
                        treeMenu.setUrl(menus.get(i).getMenuUrl());
                        treeMenu.setIconCls(menus.get(i).getIcons());
                        treeMenu.setChildren(getCurrentChildrenMenus(menus, menus.get(i).getMenuId()));
                        treeMenus.add(treeMenu);
                    }
                }
            }
        }
        return treeMenus;
    }

    /**
     * get all menus
     */
    /**
     * getAllMenus
     * @param menus
     * @return
     */
    public static List<TreeMenu> getAllMenus(List<Menu> menus) {
        List<TreeMenu> treeMenus = new ArrayList<>();
        TreeMenu treeMenu;
        if (menus.size() != 0) {
            for (int i = 0; i < menus.size(); i++) {
                if (!StringUtils.isEmpty(menus.get(i).getParentId()) && "0".equals(menus.get(i).getParentId())) {
                    treeMenu = new TreeMenu();
                    treeMenu.setId(menus.get(i).getMenuId());
                    treeMenu.setText(menus.get(i).getMenuName());
                    treeMenu.setIconCls(menus.get(i).getIcons());
                    treeMenu.setChildren(getChildrenMenus(menus, menus.get(i).getMenuId()));
                    treeMenus.add(treeMenu);
                }
            }
        }
        return treeMenus;
    }

    private static List<TreeMenu> getChildrenMenus(List<Menu> menus, String menuId) {
        List<TreeMenu> treeMenus = new ArrayList<>();
        TreeMenu treeMenu;
        if (menus.size() != 0) {
            for (int i = 0; i < menus.size(); i++) {
                if (!StringUtils.isEmpty(menus.get(i).getParentId()) && 0 != Integer.parseInt(menus.get(i).getParentId())) {
                    if (menuId.equals(menus.get(i).getParentId()) && "0".equals(menus.get(i).getIsShow())) { // 包含按钮的
                        treeMenu = new TreeMenu();
                        treeMenu.setId(menus.get(i).getMenuId());
                        treeMenu.setParentId(menuId);
                        treeMenu.setText(menus.get(i).getMenuName());
                        treeMenu.setUrl(menus.get(i).getMenuUrl());
                        treeMenu.setIconCls(menus.get(i).getIcons());
                        treeMenu.setChildren(getChildrenMenus(menus, menus.get(i).getMenuId()));
                        treeMenus.add(treeMenu);
                    }
                }
            }
        }
        return treeMenus;
    }

    /**
     * tree role list
     */
    public static List<TreeMenu> getRoleListForTree(List<Role> roles){
        List<TreeMenu> treeMenus = new ArrayList<>();
        TreeMenu treeMenu;
        if (roles.size() != 0) {
            for (int i = 0; i < roles.size(); i++) {
                treeMenu = new TreeMenu();
                treeMenu.setId(roles.get(i).getId());
                treeMenu.setText(roles.get(i).getRoleName());
                treeMenus.add(treeMenu);
            }
        }
        return treeMenus;
    }

}
